﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.Generic;
using Telerik.Windows.Controls;
using FXC.SL.UserControls.FXCChart;
using FXC.SL.Interface;
using FXC.SL.CS.Util;

namespace FXC.SL.CS.ChartUtil
{
    public class FXProductChartManager
    {
        private static List<IChart> _chartList = new List<IChart>();

        public static event EventHandler<SelectedFXVChartEventArgs> SelectionChanged;

        #region Properties
        private static int _selectedChartIndex;
        public static int SelectedChartIndex
        {
            get
            {
                return _selectedChartIndex;
            }
            set
            {
                if (value < 0 || value >= _chartList.Count)
                    throw new IndexOutOfRangeException("设置选中的图表号超出范围！");
                _selectedChartIndex = value;
                if (SelectionChanged != null)
                    SelectionChanged(null, new SelectedFXVChartEventArgs() { SelectedFXVChart = SelectedChart });
            }
        }

        public static IChart SelectedChart
        {
            get
            {
                if (_selectedChartIndex < 0)
                    return null;
                else
                {
                    return _chartList[_selectedChartIndex];
                }
            }
        }

        public static bool IsExists(string productName)
        {
            for (int i = 0; i < _chartList.Count; i++)
            {
                if (_chartList[i].ProductName == productName)
                    return true;
            }
            return false;
        }
        #endregion

        static FXProductChartManager()
        {
            _selectedChartIndex = -1;
        }

        //public static FXProductChart AddChart(RadPaneGroup container, string productName)
        //{
        //    if (IsExists(productName))
        //        return null ;
        //    RadPane rp = new RadPane();
        //    rp.Header = productName;
        //    FXProductChart chart = new FXProductChart();
        //    chart.ProductName = productName;
        //    rp.Content = chart;
        //    if (productName == "EURAUD")
        //        chart.GetDataFromService();
            
        //    container.Items.Add(rp);
        //    //_chartList.Add(chart);

        //    _selectedChartIndex = _chartList.Count - 1;
        //    rp.GotFocus += new RoutedEventHandler(rp_GotFocus);
        //    return chart;
        //}

        public static IChart AddChart(RadPaneGroup container, string productName)
        {
            if (IsExists(productName))
                return null;
            RadPane rp = new RadPane();
            rp.Header = productName;
            //FXProductChart chart = new FXProductChart();
            //chart.ProductName = productName;
            
            //if (productName == "EURAUD")
            //    chart.GetDataFromService();
            //IChart chart = new FXChart(productName, Util.PeriodType.ThirtyMinutes);
            IChart chart = new FXVChart(productName, Util.PeriodType.T_30M);
            rp.Content = chart;
            container.Items.Add(rp);
            _chartList.Add(chart);

            SelectedChartIndex = _chartList.Count - 1;
            rp.GotFocus += new RoutedEventHandler(rp_GotFocus);
            return chart;
        }

        public static void RemoveChart(IChart cht)
        {
            if (cht == null)
                return;
            for (int i = 0; i < _chartList.Count; i++)
            {
                if(cht == _chartList[i])
                    _chartList.Remove(cht);
            }
        }

        #region RadPane Events

        static void rp_GotFocus(object sender, RoutedEventArgs e)
        {
            IChart cha = (sender as RadPane).Content as IChart;
            if(cha != null){
                for (int i = 0; i < _chartList.Count; i++)
                {
                    if (cha.ProductName == _chartList[i].ProductName)
                    {
                        SelectedChartIndex = i;
                        break;
                    }
                }
            }
        }
        #endregion
    }
}
